<script setup lang="ts">
import { useCategorystore } from '@/stores/CategoryStore.ts'
import { reqAddOrupdateattr, reqDelattr } from '@/api/product/attr'
import { Attr } from '@/api/product/attr/type.ts'

const useCategory = useCategorystore()
import { onMounted, ref, onBeforeUnmount } from 'vue'
//@ts-ignore
import { ElMessage } from 'element-plus'
onMounted(async () => {
  await useCategory.reqCategory1()
})
const scene = ref(0)
const addParams = ref<Attr>({
  attrName: '',
  attrValueList: [],
  categoryId: '',
  categoryLevel: 3,
})
const addAttr = () => {
  addParams.value.attrName = ''
  addParams.value.attrValueList = []
  scene.value = 1
}
const updateAttr = (row: any) => {
  addParams.value.attrName = ''
  addParams.value.attrValueList = []
  scene.value = 1
  addParams.value.attrName = row.attrName
  addParams.value.attrValueList = row.attrValueList
  Object.assign(addParams.value, JSON.parse(JSON.stringify(row)))
}
const cancel = () => {
  scene.value = 0
}
const save = async () => {
  if (addParams.value.categoryId === '') {
    addParams.value.categoryLevel = 3
    addParams.value.categoryId = useCategory.ID3
  }
  try {
    await reqAddOrupdateattr(addParams.value)
    if (!addParams.value.id) {
      ElMessage({
        type: 'success',
        message: '增加成功',
      })
    } else {
      ElMessage({
        type: 'success',
        message: '修改成功',
      })
    }
  } catch (e) {
    if (!addParams.value.id) {
      ElMessage({
        type: 'success',
        message: '增加失败',
      })
    } else {
      ElMessage({
        type: 'success',
        message: '修改失败',
      })
    }
  }
  await useCategory.reqCategoryList(useCategory.ID1, useCategory.ID2, useCategory.ID3,'attr')
  scene.value = 0
}
const addValue = () => {
  addParams.value.attrValueList.push({
    valueName: '',
  })
}
const delrow = (row: any) => {
  addParams.value.attrValueList = addParams.value.attrValueList.filter(
    (ele) => {
      return ele.id !== row.id
    },
  )
}
const deleteAttr = async (id: any) => {
  console.log(id)
  try {
    await reqDelattr(id)
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
  } catch (e) {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }

  await useCategory.reqCategoryList(
    useCategory.ID1,
    useCategory.ID2,
    useCategory.ID3,
      'attr'
  )
}
onBeforeUnmount(() => {
  //清空仓库的数据
  useCategory.resetState()
})
</script>

<template>
  <Category :scene="scene" type="attr"></Category>
  <el-card style="margin-top: 20px">
    <div v-show="scene === 0">
      <el-button
        icon="plus"
        type="primary"
        @click="addAttr"
        :disabled="!useCategory.isadd"
        v-has="'btn.Attr.add'"
      >
        添加属性
      </el-button>
      <el-table
        border
        style="margin: 10px 0"
        :data="useCategory.CategoryList"
        v-loading="useCategory.loading"
      >
        <el-table-column
          label="序号"
          width="80px"
          align="center"
          type="index"
        ></el-table-column>
        <el-table-column
          label="属性名称"
          width="150px"
          prop="attrName"
        ></el-table-column>
        <el-table-column label="属性值名称">
          <template #="{ row }">
            <el-tag
              style="margin: 5px"
              v-for="item in row.attrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template #="{ row }">
            <!-- 修改已有属性的按钮 -->
            <el-button
              type="warning"
              size="small"
              icon="Edit"
              @click="updateAttr(row)"
              v-has="'btn.Attr.update'"
            ></el-button>
            <el-popconfirm
              :title="`你确定删除这个属性吗?`"
              width="200px"
              @confirm="deleteAttr(row.id)"
            >
              <template #reference>
                <el-button type="danger" size="small" icon="Delete" v-has="'btn.Attr.remove'"></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-show="scene === 1">
      <el-form inline>
        <el-form-item label="属性名称">
          <el-input
            placeholder="请填写属性名字"
            v-model="addParams.attrName"
            required="true"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button
        type="primary"
        icon="plus"
        :disabled="addParams.attrName === ''"
        @click="addValue"
      >
        添加属性值
      </el-button>
      <el-button @click="cancel">取消</el-button>
      <el-table
        border
        style="margin-top: 10px; margin-bottom: 10px"
        :data="addParams.attrValueList"
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="80px"
        ></el-table-column>
        <el-table-column label="属性值">
          <template #="{ row }">
            <el-input
              v-model="row.valueName"
              placeholder="请输入属性值"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #="{ row }">
            <el-button
              type="danger"
              icon="delete"
              size="small"
              @click="delrow(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-card>
</template>

<style scoped lang="scss"></style>
